#include <iostream>
#include <functional>
#include <mutex>
#include <condition_variable>
#include <thread>

class FooBar {
private:
    int n;
    std::mutex mtx1,mtx2;
public:
    FooBar(int n) {
        this->n = n;
        mtx2.lock();
    }
    void foo(std::function<void()> printFoo) {
        for (int i = 0; i < n; i++) {
            mtx1.lock();
            printFoo();
            mtx2.unlock();
        }
    }
    void bar(std::function<void()> printBar) {
        for (int i = 0; i < n; i++) {
            mtx2.lock();
            printBar();
            mtx1.unlock();
        }
    }
};

int main() {
    FooBar foo(4);  // 创建 Foo 类实例

    
    std::thread t1([&foo] {foo.foo([]{ std::cout << "foo"; });});
    std::thread t2([&foo] {foo.bar([]{ std::cout<<"bar\n";});});
    // 等待三个线程完成
    t1.join();
    t2.join();

    return 0;
}